package com.xiaoxiong.gobang.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.xiaoxiong.gobang.R;

/**
 * @author xiaoxiong
 * @Package com.xiaoxiong.gobang.ui
 * @Description:游戏界面
 * @email 249900679@qq.com
 * @date 2016/4/2 20:25
 */
public class GameView extends SurfaceView implements SurfaceHolder.Callback {

    private Context mContext;
    private Canvas mCanvas;

    private Drawable CheckerBoardDrawable;
    private Drawable BlackChessDrawable;
    private Drawable WhiteChesDrawable;
    private Drawable SelectBackgroundDrawable;

    private int Width;
    private int[] boardwh;
    private int[] chesswh;
    private int[] selectwh;

    public GameView(Context context) {
        super(context);
        Init();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Init();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Init();
    }

    private void Init(){
        mContext = this.getContext();
        getHolder().addCallback(this);

        boardwh = new int[2];
        chesswh = new int[2];
        selectwh = new int[2];
        CheckerBoardDrawable = getResources().getDrawable(R.drawable.board);
        BlackChessDrawable = getResources().getDrawable(R.drawable.black);
        WhiteChesDrawable = getResources().getDrawable(R.drawable.white);
        SelectBackgroundDrawable = getResources().getDrawable(R.drawable.selected);

        boardwh[0] = CheckerBoardDrawable.getIntrinsicWidth();
        boardwh[1] = CheckerBoardDrawable.getIntrinsicHeight();
        Log.d("picture","boardwh:" + boardwh[0] + " " + boardwh[1]);
        chesswh[0] = BlackChessDrawable.getIntrinsicWidth();
        chesswh[1] = BlackChessDrawable.getIntrinsicHeight();
        Log.d("picture","chesswh:" + chesswh[0] + " " + chesswh[1]);
        selectwh[0] = SelectBackgroundDrawable.getIntrinsicWidth();
        selectwh[1] = SelectBackgroundDrawable.getIntrinsicHeight();
        Log.d("picture","selectwh:" + selectwh[0] + " " + selectwh[1]);


    }



    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }
private float scale;
    private SurfaceHolder mHolder;
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        this.Width = width;
        mHolder = holder;
        scale = ((float) Width) / boardwh[0];
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.board);
        mCanvas = holder.lockCanvas();
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Log.d("test", mCanvas + "");
        Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        mCanvas.drawBitmap(bitmap1, 0, 0, null);
        holder.unlockCanvasAndPost(mCanvas);
        Log.d("test", width + "");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    public void setXY(float x, float y){
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.black);
        int w = bitmap.getWidth();
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        w *= scale;
        x = (int) ((x - 5 * scale) * 1.0 / Width * 15);
        y = (int) ((y - 6 * scale) * 1.0 / Width * 15);
        mCanvas = mHolder.lockCanvas();


        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.board);
        int w2 = bitmap2.getWidth();
        int h2 = bitmap2.getHeight();
        Bitmap bitmap3 = Bitmap.createBitmap(bitmap2, 0, 0, w2, h2, matrix, true);
        mCanvas.drawBitmap(bitmap3, 0, 0, null);

        int wi = bitmap.getWidth();
        int hi = bitmap.getHeight();
        Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, wi, hi, matrix, true);
        mCanvas.drawBitmap(bitmap1, x * Width / 15 + 5 * scale, y * Width / 15 + 6 * scale, null);
        mHolder.unlockCanvasAndPost(mCanvas);
        Log.d("pos", x + "----" + y);
    }
}
